﻿using Gx.Common;
using Gx.Common.Core;
using Gx.Dal.Manage;
using Gx.Dal.Shared;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Gx.Bll.Manage
{
    public class LKCustomerPrePaymentBll
    {
    
        protected LKCustomerPrePaymentDal dal = new LKCustomerPrePaymentDal();
    
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public PagedList<LKCustomerPrePayment> GetLKCustomerPrePaymentPageList(LKCustomerPrePaymentInput input)
        {
            return dal.GetLKCustomerPrePaymentPageList(input);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public List<LKCustomerPrePayment> GetLKCustomerPrePayments()
        {
            return dal.GetAll().ToList();
        }

        /// <summary>
        /// 新增客户预付款
        /// </summary>
        /// <param name="lKCustomerPrePayment"></param>
        /// <returns></returns>
        public ReturnModel AddLKCustomerPrePayment(LKCustomerPrePayment lKCustomerPrePayment)
        {
            //var any = dal.AnyCustomerPrePay(lKCustomerPrePayment.CustomerNo);
            //if (any)
            //{
            //    return new ReturnModel { Success = false, Msg = "客户预付款已存在" };
            //}

            string maxCode = dal.GetMaxLKCustomerPrePaymentCode();
            string code = "R" + DateTime.Now.ToString("yyMMdd") + "001";
            if (!string.IsNullOrWhiteSpace(maxCode))
            {
                //获取240716023月份，07为月份
                var maxMonth = maxCode.Substring(2, 2);
                var nowMonth = DateTime.Now.ToString("MM");
                if (maxMonth == nowMonth)
                {
                    int num = Convert.ToInt32(maxCode);//Convert.ToInt32(maxCode.Substring(7, 3));
                    num++;
                    code = "Y" + num.ToString();//DateTime.Now.ToString("yyMMdd") + num.ToString().PadLeft(3, '0');
                }

            }
            lKCustomerPrePayment.BillNo = code;
            lKCustomerPrePayment.CreateTime = DateTime.Now;
            lKCustomerPrePayment.CreateUserId = LoginHelper.GetUserId();
            lKCustomerPrePayment.CreateUser = LoginHelper.UserName();
            lKCustomerPrePayment.CompanyId = UserContext.CompanyId;
            var result= dal.AddLKCustomerPrePayment(lKCustomerPrePayment);
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }

        /// <summary>
        /// 编辑客户预付款
        /// </summary>
        /// <param name="lKCustomerPrePayment"></param>
        /// <returns></returns>
        public bool EditLKCustomerPrePayment(LKCustomerPrePayment lKCustomerPrePayment)
        {
            var cus = dal.Get(lKCustomerPrePayment.Id);
            if (cus == null)
            {
                return false;
            }
            if (cus.CompanyId != UserContext.CompanyId) return false;
            lKCustomerPrePayment.BillNo = cus.BillNo;
            lKCustomerPrePayment.CreateTime = cus.CreateTime;
            lKCustomerPrePayment.CreateUserId = cus.CreateUserId;
            lKCustomerPrePayment.CreateUser = cus.CreateUser;
            lKCustomerPrePayment.ModifyUserId = LoginHelper.GetUserId();
            lKCustomerPrePayment.ModifyUser = LoginHelper.UserName();
            lKCustomerPrePayment.ModifyTime = DateTime.Now;
            lKCustomerPrePayment.CompanyId = UserContext.CompanyId;
            return dal.UpdateLKCustomerPrePayment(lKCustomerPrePayment);
        }

        /// <summary>
        /// 删除客户预付款
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteLKCustomerPrePayment(int id)
        {
            return dal.DeleteById(id);
        }

        /// <summary>
        /// 获取客户预付款
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LKCustomerPrePayment GetLKCustomerPrePaymentById(int id)
        {
            var bill = dal.Get(id);
            if (bill.CompanyId != UserContext.CompanyId)
                return new LKCustomerPrePayment();
            return bill;
        }
    }
}

